<%@ page pageEncoding="utf-8"%>

<%@ page import="org.jawa.learning.webapp.util.*,org.jawa.core.util.*,java.util.*,javax.mail.*,javax.mail.internet.*"%>
<%@ page import="java.text.SimpleDateFormat"%>

<jsp:useBean id="webManager" class="org.jawa.learning.webapp.util.WebManager"  /><%
	webManager.init(request, response, session, application, out);
	String path = request.getContextPath();
%>

<% // Get paramters
    boolean doTest = request.getParameter("test") != null;
    boolean cancel = request.getParameter("cancel") != null;
    boolean sent = ParamUtils.getBooleanParameter(request, "sent");
    boolean success = ParamUtils.getBooleanParameter(request, "success");
    String from = ParamUtils.getParameter(request, "from");
    String to = ParamUtils.getParameter(request, "to");
    String subject = ParamUtils.getParameter(request, "subject");
    String body = ParamUtils.getParameter(request, "body");

    // Cancel if requested
    if (cancel) {
        response.sendRedirect(path+"/admin/system-email.jsp");
        return;
    }

    // Variable to hold messaging exception, if one occurs
    Exception mex = null;

    // Validate input
    Map<String, String> errors = new HashMap<String, String>();
    if (doTest) {
        if (from == null) {
            errors.put("from", "");
        }
        if (to == null) {
            errors.put("to", "");
        }
        if (subject == null) {
            errors.put("subject", "");
        }
        if (body == null) {
            errors.put("body", "");
        }

        EmailService service = EmailService.getInstance();

        // Validate host - at a minimum, it needs to be set:
        String host = service.getHost();
        if (host == null) {
            errors.put("host", "");
        }

        // if no errors, continue
        if (errors.size() == 0) {
            // Create a message
            MimeMessage message = service.createMimeMessage();
            // Set the date of the message to be the current date
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z",
                    java.util.Locale.CHINA);
            //format.setTimeZone(JiveGlobals.getTimeZone());
            message.setHeader("Date", format.format(new Date()));

            // Set to and from.
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to, null));
            message.setFrom(new InternetAddress(from, null));
            message.setSubject(subject);
            message.setText(body);
            // Send the message, wrap in a try/catch:
            try {
                service.sendMessagesImmediately(Collections.singletonList(message));
                // success, so indicate this:
                response.sendRedirect(path+"/admin/system-emailtest.jsp?sent=true&success=true");
                return;
            }
            catch (MessagingException me) {
                me.printStackTrace();
                mex = me;
            }
        }
    }

    if (from == null) {
        from = "admin@admin.com";
    }
    if (to == null) {
        to = "admin@admin.com";
    }
    if (subject == null) {
        subject = "这是一份来自于电子学习系统的测试邮件!";
    }
    if (body == null) {
        body = "这是一份来自于电子学习系统的测试邮件!";
    }
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>邮件管理</title>
		<meta name="navID" content="1000" />
		<meta name="pageID" content="1202" />
	</head>
	<body>

		<div class="page-header">
			<h4>
				发送测试邮件
			</h4>
		</div>
		<%  if (JawaGlobals.getProperty("mail.smtp.host") == null) { %>

		<div class="alert alert-error fade in">
			<a class="close" data-dismiss="alert" href="#">×</a>
			<i class="icon-ok-circle icon-white"></i><strong>发送测试邮件失败,可能是邮件服务器不正确!</strong>
		</div>

		<%  } %>

		<%  if (doTest || sent) { %>

		<%  if (success) { %>

		<div class="alert alert-success fade in">
			<a class="close" data-dismiss="alert" href="#">×</a>
			<i class="icon-ok-circle icon-white"></i><strong>发送测试邮件成功!</strong>
		</div>
		<%  } else { %>

		<div class="alert alert-error fade in">
			<a class="close" data-dismiss="alert" href="#">×</a>
			<i class="icon-ok-circle icon-white"></i><strong>发送测试邮件失败,可能是验证信息不正确!</strong>
		</div>

		<%  } %>

		<%  } %>

		<form action="<%=path %>/admin/system-emailtest.jsp" method="post" name="f" onsubmit="return checkClick(this);" class="form-horizontal well">
		         <div class="control-group <%=errors.containsKey("host") ? "error" : ""%>" >
						<label for="focusedInput" class="control-label">
							邮件服务器:
						</label>
						<div class="controls">
						    <span class="input-xlarge uneditable-input"> 
							<%  String host = JawaGlobals.getProperty("mail.smtp.host");
				                if (host == null) {
				            %>
							没有设置邮件服务器
							<%
				                } else {
				            %>
							<%= host %>:<%= JawaGlobals.getIntProperty("mail.smtp.port", 25)  %>
							<%  if (JawaGlobals.getBooleanProperty("mail.smtp.ssl", false)) { %>
							(
							使用SSL
							)
							<%  } %>
							<%  } %>
							</span>
						</div>
				</div>
				<div class="control-group ">
						<label for="focusedInput" class="control-label">
							来自于:
						</label>
						<div class="controls">
							<input type="hidden" name="from" value="<%= from %>" class="input-xlarge "  />
							<span class="input-xlarge uneditable-input"> <%= StringUtils.escapeHTMLTags(from) %></span>
						</div>
				</div>
				<div class="control-group ">
						<label for="focusedInput" class="control-label">
							发送到:
						</label>
						<div class="controls">
							<input type="text" name="to"
								value="<%= ((to != null) ? to : "") %>" size="40"
								maxlength="100" class="input-xlarge " >
						</div>
				</div>
				<div class="control-group ">
						<label for="focusedInput" class="control-label">
							主题:
						</label>
						<div class="controls">
							<input type="text" name="subject"
								value="<%= ((subject != null) ? subject : "") %>" size="50"
								maxlength="100" class="input-xlarge " >	
						</div>
				</div>
				<div class="control-group ">
						<label for="focusedInput" class="control-label">
							内容:
						</label>
						<div class="controls">
							<textarea name="body" cols="45" rows="5" class="span6" ><%= body %></textarea>	
						</div>
				</div>
			    <div class="form-actions">
						<input type="submit" class="btn btn-primary" name="test" value="发送">
						<input type="submit" class="btn" name="cancel" value="取消/返回">
				</div>
		</form>

	</body>
	<script src="<%= path %>/scripts/bootstrap.js"></script>
	<script src="<%= path %>/scripts/jquery.validate.js"></script>
	<script>
	var clicked = false;
	function checkClick(el) {
		if (!clicked) {
			clicked = true;
			return true;
		}
		return false;
	}
</script>
</html>